#include "StdAfx.h"
#include "VSqlTableView.h"

#include "VSqlDataView.h"
#include "VSqlMetaView.h"

// VSqlTableView //////////////////////////////////////////////////////////////

//
VSqlTableView::VSqlTableView(VSqlMetaModel *metaModel,QWidget *parent)
	:QFrame(parent)
	,m_metaModel(metaModel)
{
	m_dataModel = new VSqlDataModel(metaModel->metaMap(),this);
	m_dataView = new VSqlDataView(this);
		m_dataView->setModel(m_dataModel);

	m_dataRefModel = new VSqlDataModel(metaModel->metaMap(),this);
	m_dataRefView = new VSqlDataView(this);
		m_dataRefView->setModel(m_dataRefModel);


	m_vSplitter = new QSplitter(this);
		m_vSplitter->setOrientation(Qt::Vertical);
		
	m_dataInfo = new QLabel(this);
	//QSizePolicy policy = m_dataInfo->sizePolicy();
	//	policy.setVerticalStretch(stretch);
	//m_dataInfo->setSizePolicy(policy);

	
	QVBoxLayout *l = new QVBoxLayout;//(this);
		l->setContentsMargins(0,0,0,0);
		l->addWidget(m_dataInfo);
		l->addWidget(m_dataView);
	QFrame *f = new QFrame(this);
		f->setLayout(l);
		m_vSplitter->addWidget(f);
		
	m_dataRefInfo = new QLabel(this);
	l = new QVBoxLayout;//(this);
		l->setContentsMargins(0,0,0,0);
		l->addWidget(m_dataRefInfo);
		l->addWidget(m_dataRefView);
	m_dataRefFrame = new QFrame(this);
		m_dataRefFrame->setLayout(l);
		m_vSplitter->addWidget(m_dataRefFrame);


	connect(
		m_dataView,SIGNAL(
			currentChanged(const VSqlDataColumn &,const QVariant &)
		),
		this,SLOT(
			dataViewCurrentChanged(const VSqlDataColumn &,const QVariant &)
		)
	);

	////////////////////////////////////////////////
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
		mainLayout->setContentsMargins(0,0,0,0);
		mainLayout->addWidget(m_vSplitter);
	setLayout(mainLayout);

	VSettings settings(VSqlTableView::staticMetaObject.className());
		m_vSplitter->restoreState(
			settings.value("vSplitter/state",m_vSplitter->saveState()).toByteArray()
		);
}

//
VSqlTableView::~VSqlTableView()
{
	VSettings settings(VSqlTableView::staticMetaObject.className());
		settings.setValue("vSplitter/State",m_vSplitter->saveState());
}

//
void VSqlTableView::setTable(const VSqlMeta &meta_)
{
	m_dataRefFrame->setVisible(false);

	VSqlMeta meta = meta_;

	if (meta.item().isField())
		meta = m_metaModel->metaMap()->meta_byIdNode(meta.node().idNodeParent().value());

	if (meta.item().isTable())
	{
		m_dataInfo->setText(QString()
			+ "<h3><i>"
			+ meta.item().displayName().value().toString()
			+ "</i></h3>"
		);
		m_dataModel->setTable(meta);//meta.item().keyName().value().toString());
		//this->setVisible(true);
	}
	else
	{
		//this->setVisible(false);
		m_dataInfo->setText("");
		m_dataModel->clear();
	}

	m_dataView->view()->resizeRowsToContents();
	if (m_dataRefModel)
		m_dataRefModel->clear();
}

//
void VSqlTableView::dataViewCurrentChanged(const VSqlDataColumn &dataColumn,const QVariant &value)
{
	if (m_dataRefModel && m_dataRefView)
	{
		VSqlItem itemColumn = dataColumn.meta().item();
		if (!itemColumn.isRef())
		{
			m_dataRefFrame->setVisible(false);
			m_dataRefInfo->setText("");
			m_dataRefModel->clear();
			
			return;
		}

		VSqlItem itemRefTable = m_metaModel->metaMap()->item_byIdItem(
			itemColumn.refIdItem().value()
		);
		QString keyNameRefTable = itemRefTable.keyName().value().toString();
		VSqlMeta metaRefTable = m_metaModel->metaMap()->meta_byKeyName(keyNameRefTable);

		m_dataRefInfo->setText(QString()
			+ "<h3><i>"
			+ itemColumn.displayName().value().toString()
			+ " ("
			+ itemRefTable.displayName().value().toString()
			+ ")"
			+ "</i></h3>"
		);

		QString sValue = value.toString();
		if (sValue.isEmpty())
			sValue = "-1";

		QSqlField idRecField = m_dataModel->sqlData()->idRecField(); // ?? m_dataRefModel
		QString sqlWhere = idRecField.name() + " in(" + sValue + ")";

		m_dataRefModel->setTable(metaRefTable,sqlWhere);
		m_dataRefView->view()->resizeRowsToContents();
		m_dataRefFrame->setVisible(true);
	}
}
